#
# PVCSConstants.py
#
# this file defines some global constants and functions
# for the PVCS integration
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# set Preferences File
import searchPathLib, os
PVCSPrefsFileName = os.path.normpath(os.path.join(searchPathLib.getUsersProfileDir(), "PvcsPrefs.sniff"))

# global debug variables
import PVCSCommonHelp
TEST = int(PVCSCommonHelp.readValueFromPrefsFile(PVCSPrefsFileName, "PVCSDebug"))
VLOGTEST = int(PVCSCommonHelp.readValueFromPrefsFile(PVCSPrefsFileName, "VLOGDebug"))

# adapter specific subdirectory
AdapterSpecificSubDir = PVCSCommonHelp.readValueFromPrefsFile(PVCSPrefsFileName, "AdapterSubDir")

if TEST:
	print "(PVCS Constants) TEST = '%d'" % TEST
	print "(PVCS Constants) VLOGTEST = '%d'" % VLOGTEST

# initialize Call Interface
import CommonCallWrapper
usedCallingInterface = CommonCallWrapper.LOCAL
callWrapper = None
if os.environ.has_key("SNiFF_CMVC_USE_CALL_INTERFACE"):
	usedCallingInterface = int(os.environ["SNiFF_CMVC_USE_CALL_INTERFACE"])
try:
	callWrapper = CommonCallWrapper.CommonCallWrapper("Pvcs", usedCallingInterface)
except CommonCallWrapper.CommonCallWrapperInitFailed:
	usedCallingInterface = CommonCallWrapper.LOCAL
except CommonCallWrapper.CommonCallWrapperInterfaceImportFailed:
	usedCallingInterface = CommonCallWrapper.LOCAL

# the boolean values, use the definitions of vcs.py
# if available
vcs_module_imported = 1
try:
	import vcs
	true = vcs.TRUE
	false = vcs.FALSE
	if not os.environ.has_key("VCSDEBUG"):
		if PVCSCommonHelp.readValueFromPrefsFile(PVCSPrefsFileName, "VCSDebug") == "1":
			vcs.TEST = "1"
		else:
			vcs.TEST = ''
	if TEST:
		print "(PVCS Constants) vcs.TEST = '%s'" % vcs.TEST

	# define a extended unixpath
	def unixpath(path):
		if not path: return path
		import os
		path = os.path.normpath(path)
		import string
		path = string.translate(path, vcs.BACKSLASH2SLASH)
		if path[-1]=='/':
			return path[:-1]
		return path
except ImportError:
	# vcs.py module not available
	# define it by ourself
	true = 1
	false = 0
	vcs_module_imported = 0

# a different split function, DON'T use string.split !
def split(s, sep=None, maxsplit=0):
	"""split(str [,sep [,maxsplit]]) -> list of strings

	Return a list of the words in the string s, using sep as the
	delimiter string.  If maxsplit is nonzero, splits into at most
	maxsplit words If sep is not specified, any whitespace string
	is a separator.  Maxsplit defaults to 0.
	"""
	import string
	if sep is None: sep = string.whitespace
	res = []
	i, n = 0, len(s)
	if maxsplit <= 0: maxsplit = n
	count = 0
	while i < n:
		while i < n and s[i] in sep: i = i+1
		if i == n: break
		if count >= maxsplit:
			res.append(s[i:])
			break
		j = i
		while j < n and s[j] not in sep: j = j+1
		count = count + 1
		res.append(s[i:j])
		i = j
	return res

# this function translate the PVCS meta notation
# into regular expressions
def makeRegEx(aCoreString):
	aRegExString = r""
	for aCharIndex in range(len(aCoreString)):
		if aCoreString[aCharIndex] == '.':
			# translate '.' to '\.'
			aRegExString = aRegExString + r"\."
		else:
			if aCoreString[aCharIndex] == '?':
				# translate '?' to '.'
				aRegExString = aRegExString + r"."
			else:
				if aCoreString[aCharIndex] == '*':
					# translate '*' to '[0-9]*'
					aRegExString = aRegExString + r"[0-9]*"
				else:
					# append the normal character
					aRegExString = aRegExString + aCoreString[aCharIndex]
	return aRegExString + r"$"

# this function returns the current tempdir
# it looks for the environment variables TMPDIR, TEMP and TMP
# in this order, if no variable present, current working directory is used
def getTempDir():
	import os
	retTempDir = os.getcwd()
	# check TMPDIR
	if os.environ.has_key("TMPDIR"):
		retTempDir = os.environ["TMPDIR"]
	else:
		if os.environ.has_key("tmpdir"):
			retTempDir = os.environ["tmpdir"]
		else:
			if os.environ.has_key("TEMP"):
				retTempDir = os.environ["TEMP"]
			else:
				if os.environ.has_key("temp"):
					retTempDir = os.environ["temp"]
				else:
					if os.environ.has_key("TMP"):
						retTempDir = os.environ["TMP"]
					else:
						if os.environ.has_key("tmp"):
							retTempDir = os.environ["tmp"]
	# check, if we can write to the calcualted temp dir
	if vcs_module_imported == 1:
		if not vcs.is_writable(retTempDir):
			# use default locations
			if sys.platform == "win32":
				# try to get system drive
				if os.environ.has_key("SystemDrive"):
					retTempDir=os.environ["SystemDrive"] + "/Temp"
				else:
					retTempDir="C:/Temp"
			else:
				retTempDir="/tmp"
	else:
		if retTempDir=="":
			if sys.platform == "win32":
				# try to get system drive
				if os.environ.has_key("SystemDrive"):
					retTempDir=os.environ["SystemDrive"] + "/Temp"
				else:
					retTempDir="C:/Temp"
			else:
				retTempDir="/tmp"
	return os.path.normpath(retTempDir)

# this function returns a unique temoprary filename
def getTempFile():
	import tempfile, os
	tempfile.tempdir = getTempDir()
	tempfile.template = "pvcs_tmp_"
	return os.path.normpath(tempfile.mktemp())

# the exception class definitions
import exceptions
class PVCSConfigFileSyntaxError(exceptions.StandardError):
	pass

class PVCSNoSuffixRule(exceptions.StandardError):
	pass

class PVCSErrorWritingConfigFile(exceptions.StandardError):
	pass
